﻿using BUSINESS;
using BUSINESS.ExcelService;
using DataAccessObject;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI.HocSinh
{
    /// <summary>
    /// Interaction logic for GUITiepNhanHocSinh.xaml
    /// </summary>
    public partial class GUITiepNhanHocSinh : Window
    {
        List<KHOILOP> listKhoiLop;
        List<HOCSINH> listHocSinh;
        List<LOPHOC> listLopHoc;
        public GUITiepNhanHocSinh()
        {
            InitializeComponent();
            /////////////////////
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeControl();
        }

        private void InitializeControl()
        {
            listKhoiLop = KhoiLopBUS.getAllKhoiLop();
            txtInputListKhoiLop.ItemsSource = listKhoiLop;
            /////////////////////////////////////////////
            txtInputNamHoc.ItemsSource = NamHocBUS.getAllNamHoc();
            if (txtInputNamHoc.Items.Count > 0) txtInputNamHoc.SelectedIndex = 0;
            //////////////////////////////////////////////
            listHocSinh = HocSinhBUS.getAllHocSinhNoneMaHocSinh(txtInputNamHoc.SelectedItem as NAMHOC);
            tbHocSinh.ItemsSource = listHocSinh;
            //////////////////////////////////////////////
            listLopHoc = new List<LOPHOC>();
            txtInputListLopHoc.ItemsSource = listLopHoc;
        }

        private void tbHocSinh_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var hocsinh = tbHocSinh.SelectedItem as HOCSINH;
            if (hocsinh == null) return;
            ///////////////////////////////////////////
            txtInputTenHocSinh.Text = hocsinh.HoTen;
            txtInputMaHocSinh.Text = hocsinh.MaHocSinh;
            ////////////////////////////////////////////
            var khoilop = listKhoiLop.FirstOrDefault(p => p.KhoiLopId == hocsinh.KhoiLopId);
            if (khoilop != null)
            {
                txtInputListKhoiLop.SelectedItem = khoilop;
            }
            else 
            {
                txtInputListKhoiLop.SelectedIndex = -1;
            }
            //////////////////////////////////////////
        }

        private void txtInputListKhoiLop_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtError.Text = "";
            var khoilop = txtInputListKhoiLop.SelectedItem as KHOILOP;
            if (khoilop == null)
            {
                listLopHoc.Clear();
            }
            else
            {
                listLopHoc.Clear();
                listLopHoc.AddRange(LopHocBUS.getLopHocByKhoiLop(khoilop));
            }
            txtInputListLopHoc.Items.Refresh();
        }

        private void btnXacNhan_Click(object sender, RoutedEventArgs e)
        {
            var hocsinh = tbHocSinh.SelectedItem as HOCSINH;
            if (hocsinh == null) return;
            var mahocsinh = txtInputMaHocSinh.Text;
            if (mahocsinh == "" || mahocsinh == null) return;
            var lophoc = txtInputListLopHoc.SelectedItem as LOPHOC;
            if (lophoc == null) return;
            string error = LopHocBUS.insertHocSinh(lophoc, hocsinh);
            if (error == string.Empty) hocsinh.MaHocSinh = mahocsinh;
            setError(error);
            listHocSinh.Remove(hocsinh);
            tbHocSinh.Items.Refresh();
        }

        private void setError(string error)
        {
            if (error == string.Empty)
            {
                txtError.Text = "*Cập nhật thông tin thành công!";
                txtError.Foreground = Brushes.Blue;
            }
            else
            {
                txtError.Text = error;
                txtError.Foreground = Brushes.Red;
            }
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "Excel File(.xls, .xlsx)|*.xls;*.xlsx";
            if (openFile.ShowDialog() == true)
            {
                string pathname = openFile.FileName;
                var list = HocSinhExcel.ReadHocSinhFromFile(pathname);
                listHocSinh.AddRange(list);
                tbHocSinh.Items.Refresh();
            }
        }
    }
}
